package br.com.players.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import br.com.players.model.TImePlayerDTO;
import br.com.players.model.TimePlayer;

public class TimePlayerService extends PlayersService {
	
	public List<TImePlayerDTO> listarMeusTimes(String idPlayer) {
		final Query query = em.createQuery("select tp from TimePlayer tp where tp.idPlayer = :idPlayer");
		query.setParameter("idPlayer", Long.parseLong(idPlayer));
		
		List<TimePlayer> timePlayer = query.getResultList();
		
		Query meusTimes = em.createQuery("select tp from TimePlayer tp where tp.id = :id");
		
		List<TImePlayerDTO> result = new ArrayList<TImePlayerDTO>();
		
		for(TimePlayer meusT : timePlayer) {
			meusTimes.setParameter("id", meusT.getKey());
			
			List<TimePlayer> todosPlayers = meusTimes.getResultList();
			
			CadastroPlayerService cps = new CadastroPlayerService();
			CadastroTimeService cts = new CadastroTimeService();
			
			for(TimePlayer tp: todosPlayers) {
				TImePlayerDTO tpd = new TImePlayerDTO();
				tpd.setIdPlayer(tp.getIdPlayer());
				tpd.setId(tp.getKey());
				tpd.setIdTime(tp.getIdTime());
				tpd.setNomePlayer(cps.buscarPlayerPeloid(String.valueOf(tp.getIdPlayer())).getNome());
				tpd.setNomeTime(cts.buscarTimePeloId(tp.getIdTime()).getNome());
				
				result.add(tpd);
			}
		}
		return result;
	}

}
